package kafe;

import java.beans.Statement;
import java.sql.DriverManager;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.ResultSet;

public class TampilDataTabel extends JFrame{
	
	java.sql.Connection kon;
	String[] judul = {"Kode Menu","Nama Menu", "Jenis", "Harga"};
	DefaultTableModel def;
	JTable tab = new JTable();
	JScrollPane skrol = new JScrollPane();
	JPanel panel = new JPanel();
	
	public TampilDataTabel() {
		// TODO Auto-generated constructor stub
		super("Tampil Data ke Tabel Gui");
		setSize(440,300);
		panel.setLayout(null);
		def = new DefaultTableModel(null, judul);
		tab.setModel(def);
		skrol.getViewport().add(tab);
		tab.setEnabled(true);
		skrol.setBounds(20, 40, 400, 200);
		panel.add(skrol);
		getContentPane().add(panel);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		tampil();	
	
	}

	void tampil() {
		// TODO Auto-generated method stub
		try{
			Class.forName("com.mysql.jdbc.Driver");
			kon = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_kafe?user=root&password=");
			java.sql.Statement stat = kon.createStatement();
			String sql = "select kode_menu, nama_menu, kode_jenis, harga from tbl_menu";
			java.sql.ResultSet res = stat.executeQuery(sql);
			
			while(res.next()){
				String skode = res.getString("kode_menu");
				String snama = res.getString("nama_menu");
				String sjenis = res.getString("kode_jenis");
				String sharga = res.getString("harga");
				String[] data = {skode,snama,sjenis,sharga};
				def.addRow(data);
			}
			res.close();
			kon.close();
		} catch (Exception exc){
			exc.printStackTrace();
		}
		
	}
		public static void main(String[] args) {
			TampilDataTabel frameTabel = new TampilDataTabel();
		}
	

}
